package com.cook.controller.admin;

import com.cook.result.Result;
import com.cook.service.AdminService;
import com.cook.service.UserService;
import com.cook.vo.admin.AllUserDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 分页查询所有用户列表
     */
    @GetMapping("/list")
    public Result<Map<String, Object>> getAllUsersWithPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("管理员分页查询用户列表: pageNum={}, pageSize={}", pageNum, pageSize);

        try {
            Map<String, Object> result = adminService.getAllUsersWithPage(pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("查询用户列表失败: ", e);
            return Result.error(500, "查询用户列表失败");
        }
    }

    /**
     * 查询用户详情
     */
    @GetMapping("/{userId}")
    public Result<AllUserDetailVO> getUserDetailById(@PathVariable Long userId) {
        log.info("管理员查询用户详情: userId={}", userId);

        try {
            AllUserDetailVO userDetail = adminService.getUserDetailById(userId);
            return Result.success(userDetail);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询用户详情失败: ", e);
            return Result.error(500, "查询用户详情失败");
        }
    }

    /**
     * 删除用户（逻辑删除）
     */
    @DeleteMapping("/{userId}")
    public Result<Boolean> deleteUser(@PathVariable Long userId) {
        log.info("删除用户: userId={}", userId);

        try {
            boolean success = adminService.deleteUser(userId);
            if (success) {
                return Result.success("用户删除成功", true);
            } else {
                return Result.error(500, "用户删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除用户失败: ", e);
            return Result.error(500, "删除用户失败");
        }
    }

    /**
     * 搜索用户
     */
    @GetMapping("/search")
    public Result<Map<String, Object>> searchUsers(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        log.info("搜索用户: keyword={}, pageNum={}, pageSize={}", keyword, pageNum, pageSize);

        try {
            Map<String, Object> result = adminService.searchUsers(keyword, pageNum, pageSize);
            return Result.success(result);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("搜索用户失败: ", e);
            return Result.error(500, "搜索用户失败");
        }
    }

    /**
     * 获取用户总数
     */
    @GetMapping("/count")
    public Result<Integer> getUserTotalCount() {
        log.info("查询用户总数");

        try {
            int count = adminService.getUserTotalCount();
            return Result.success(count);
        } catch (Exception e) {
            log.error("查询用户总数失败: ", e);
            return Result.error(500, "查询用户总数失败");
        }
    }
}
